//////////////////////////////////////////////////////////////////////////
// $Id: MainFrame.cpp,v 1.6 2009/01/09 15:29:18 sunjun Exp $
//////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MainApp.h"
#include "MainFrame.h"
#include "NewMapDlg.h"

//////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////////
// MainFrame

IMPLEMENT_DYNAMIC(MainFrame, CFrameWndEx)
BEGIN_MESSAGE_MAP(MainFrame, CFrameWndEx)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SETFOCUS()
	ON_COMMAND(ID_VIEW_WORKSPACE_PANE, &MainFrame::OnViewWorkspacePane)
	ON_UPDATE_COMMAND_UI(ID_VIEW_WORKSPACE_PANE, &MainFrame::OnUpdateViewWorkspacePane)
	ON_COMMAND(ID_VIEW_OUTPUT_PANE, &MainFrame::OnViewOutputPane)
	ON_UPDATE_COMMAND_UI(ID_VIEW_OUTPUT_PANE, &MainFrame::OnUpdateViewOutputPane)
	ON_COMMAND(ID_VIEW_STANDARD_BAR, &MainFrame::OnViewStandardBar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_STANDARD_BAR, &MainFrame::OnUpdateViewStandardBar)
	ON_COMMAND(ID_VIEW_TOOL_BAR, &MainFrame::OnViewToolBar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_TOOL_BAR, &MainFrame::OnUpdateViewToolBar)
	ON_COMMAND(ID_VIEW_STATUS_BAR, &MainFrame::OnViewStatusBar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_STATUS_BAR, &MainFrame::OnUpdateViewStatusBar)
	ON_COMMAND(ID_FILE_NEW, &MainFrame::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, &MainFrame::OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE, &MainFrame::OnFileSave)
	ON_COMMAND(ID_EDIT_UNDO, &MainFrame::OnEditUndo)
	ON_COMMAND(ID_EDIT_REDO, &MainFrame::OnEditRedo)
	ON_COMMAND(ID_EDIT_COPY, &MainFrame::OnEditCopy)
	ON_COMMAND(ID_EDIT_CUT, &MainFrame::OnEditCut)
	ON_COMMAND(ID_EDIT_PASTE, &MainFrame::OnEditPaste)	
	ON_COMMAND(ID_EDIT_DELETE, &MainFrame::OnEditDelete)	
	ON_COMMAND(ID_EDIT_FIND, &MainFrame::OnEditFind)
	ON_COMMAND_RANGE(ID_TOOL_SELECT, ID_TOOL_TERRAIN_BRUSH_HOLE, OnToolBar)
	ON_UPDATE_COMMAND_UI_RANGE(ID_TOOL_SELECT, ID_TOOL_TERRAIN_BRUSH_HOLE, OnUpdateToolBar)		
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

// MainFrame construction/destruction

MainFrame::MainFrame()
{
	
}

MainFrame::~MainFrame()
{

}

int MainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWndEx::OnCreate(lpCreateStruct) == -1) return -1;
	
	CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerVS2005));	
	CDockingManager::SetDockingMode(DT_SMART);	
	EnableAutoHidePanes(CBRS_ALIGN_ANY);
	CMFCToolBarComboBoxButton::SetFlatMode();

	// create all of the bar
	{
		// create the menu bar
		if (!mMenuBar.Create(this,AFX_DEFAULT_TOOLBAR_STYLE,IDR_MAINFRAME)) return -1;
		mMenuBar.SetPaneStyle(mMenuBar.GetPaneStyle() | CBRS_SIZE_DYNAMIC | CBRS_TOOLTIPS | CBRS_FLYBY);

		// Menu will not take the focus on activation:
		CMFCPopupMenu::SetForceMenuFocus(FALSE);

		// create the standard tool bar	
		if (!mStandardBar.CreateEx(this, TBSTYLE_FLAT, 
			WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC,
			CRect(0,0,0,0),ID_VIEW_STANDARD_BAR) ||
			!mStandardBar.LoadToolBar(IDT_STANDARD_BAR, 0, 0, FALSE, 0, 0, IDB_STANDARD_BAR)) return -1;
		CString strStandardName;
		strStandardName.LoadString(IDS_TOOLBAR_STANDARD);
		mStandardBar.SetWindowText(strStandardName);		
		mStandardBar.ReplaceButton(ID_EDIT_FIND, CMFCToolBarComboBoxButton(ID_EDIT_FIND, GetCmdMgr()->GetCmdImage(ID_EDIT_FIND), CBS_DROPDOWN));

		// create the tool tool bar	
		if (!mToolBar.Create(this,
			WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC, 
			ID_VIEW_TOOL_BAR) ||
			!mToolBar.LoadToolBar (IDT_TOOL_BAR, 0, 0, TRUE, 0, 0, IDB_TOOL_BAR )) return -1;		
		CString strToolName;
		strToolName.LoadString(IDS_TOOLBAR_TOOL);
		mToolBar.SetWindowText(strToolName);		

		// create the status bar
		if (!mStatusBar.Create(this)) return -1;
		mStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));

		// delete these five lines if you don't want the toolbar and menubar to be dockable
		mMenuBar.EnableDocking (CBRS_ALIGN_ANY);
		mStandardBar.EnableDocking (CBRS_ALIGN_TOP);
		mToolBar.EnableDocking (CBRS_ALIGN_TOP);
		EnableDocking(CBRS_ALIGN_ANY);
		DockPane(&mMenuBar);
		DockPane(&mStandardBar);
		DockPane(&mToolBar);
	}	

	// create all of the view
	{
		// create a view to occupy the client area of the frame
		if (!mMainView.Create( AfxRegisterWndClass(CS_DBLCLKS, 
			::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_BTNSHADOW), NULL), 
			NULL, AFX_WS_DEFAULT_VIEW, CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST, NULL)) return -1;

		CString strPaneName;

		// create the workspace pane		
		strPaneName.LoadString(IDS_WORKSPACE_PANE);
		if (!mWorkspacePane.Create(strPaneName, this, CRect(0,0,250,500), TRUE, ID_WORKSPACE_PANE, 
			WS_CHILD|WS_VISIBLE|CBRS_RIGHT|CBRS_HIDE_INPLACE|WS_CAPTION))return -1;
		mWorkspacePane.SetFont(CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT)));

		// create the output pane
		CString strOutputPane;
		strOutputPane.LoadString(IDS_OUTPUT_PANE);
		if (!mOutputPane.Create(strOutputPane, this, CRect(0, 0, 400, 150), TRUE, ID_OUTPUT_PANE, 
			WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|CBRS_BOTTOM|CBRS_FLOAT_MULTI)) return -1;	
		mOutputPane.SetFont(CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT)));
		// mOutputPane.SetIcon((HICON) ::LoadImage(::AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_NODE_PANE), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), 0), FALSE);

		// dock the pane
		mWorkspacePane.EnableDocking(CBRS_ALIGN_LEFT|CBRS_ALIGN_RIGHT);
		DockPane(&mWorkspacePane);
		mOutputPane.EnableDocking(CBRS_ALIGN_TOP|CBRS_ALIGN_BOTTOM);
		DockPane(&mOutputPane);
	}

	// init the main view
	mMainView.init();

	return 0;
}

void MainFrame::OnDestroy()
{
	// exit the main view
	mMainView.exit();

	CFrameWndEx::OnDestroy();	
}

BOOL MainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWndEx::PreCreateWindow(cs) )
		return FALSE;

	cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
	cs.lpszClass = AfxRegisterWndClass(0);

	return TRUE;
}

// MainFrame message handlers
void MainFrame::OnSetFocus(CWnd* pOldWnd)
{
	mMainView.SetFocus();
}

BOOL MainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
	// let the view have first crack at the command
	if (mMainView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo)) return TRUE;

	// otherwise, do default handling
	return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

void MainFrame::OnViewWorkspacePane()
{
	mWorkspacePane.ShowPane(!mWorkspacePane.IsVisible(), FALSE, TRUE);
}

void MainFrame::OnUpdateViewWorkspacePane(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(mWorkspacePane.IsVisible());
}

void MainFrame::OnViewOutputPane()
{
	mOutputPane.ShowPane(!mOutputPane.IsVisible(), FALSE, TRUE);
}

void MainFrame::OnUpdateViewOutputPane(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(mOutputPane.IsVisible());
}

void MainFrame::OnViewStandardBar()
{
	mStandardBar.ShowPane(!mStandardBar.IsVisible(), FALSE, TRUE);
}

void MainFrame::OnUpdateViewStandardBar(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(mStandardBar.IsVisible());
}

void MainFrame::OnViewToolBar()
{
	mToolBar.ShowPane(!mToolBar.IsVisible(), FALSE, TRUE);
}

void MainFrame::OnUpdateViewToolBar(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(mToolBar.IsVisible());	
}

void MainFrame::OnViewStatusBar()
{
	mStatusBar.ShowPane(!mStatusBar.IsVisible(), FALSE, TRUE);
}

void MainFrame::OnUpdateViewStatusBar(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(mStatusBar.IsVisible());	
}

void MainFrame::OnFileNew()
{
	NewMapDlg dlg( this );
	int id = (int) dlg.DoModal();
	if (id == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (id == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}
}

void MainFrame::OnFileOpen()
{

}
void MainFrame::OnFileSave()
{

}

void MainFrame::OnEditUndo()
{

}

void MainFrame::OnEditRedo()
{

}

void MainFrame::OnEditCopy()
{

}

void MainFrame::OnEditCut()
{

}

void MainFrame::OnEditPaste()
{

}

void MainFrame::OnEditDelete()
{
	
}

void MainFrame::OnEditFind()
{
	
}

void MainFrame::OnToolBar(UINT id)
{
	gToolType = id;
}

void MainFrame::OnUpdateToolBar(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck (pCmdUI->m_nID == gToolType);
}

//////////////////////////////////////////////////////////////////////////



